var basePath='http://localhost:8080/';
var oldPhotoNum=0;
$(function(){
	initOrderInfo($("#fixOrderId").html());
	initUpload();
});
//初始化订单信息
function initOrderInfo(orderId){
	$.ajax({
		url:basePath+'fixOrder/getInfo',
		method:'get',
		dataType:'json',
		data:{
			fixOrderId:orderId,
		},
		success:function(data){
			if(data.code==0){
				setOrderInfo(data.row);
			}
			else{
				alert('获取订单信息失败')
			}
		},
		error:function(){
			alert("获取报修信息出现错误")
		}
	})
}
function setOrderInfo(info){
	var fixOrder=info.fixOrder;
	var fileList=info.photoList;
	var handleInfo=info.fixOrderHandle;
	setBaseInfo(fixOrder,fileList);
	if(handleInfo!=null&&handleInfo!=undefined)
	  setHandleInfo(handleInfo);
}
function setBaseInfo(fixOrder,fileList){
	$('#article').val(fixOrder.article);
	$('#description').val(fixOrder.description);
	$('#name').val(fixOrder.name);
	$('#phone').val(fixOrder.phone);
	$('#address').val(fixOrder.address);
	$('#userRemark').val(fixOrder.remark)
	
	for(var i=0;i<fileList.length;i++){
		var item=fileList[i];
		var webPath=basePath+item.path+'/'+item.fileName
		var html='<img src="'+webPath+'" class="photo">';
		var index=i+1;
		$("#photo"+index).parent().find('input[type="hidden"]').val(item.id);
		$("#photo"+index).empty();
		$("#photo"+index).html(html);
		oldPhotoNum++;
	}
}
function setHandleInfo(handleInfo){
	//$('#handleId').val(handleInfo.id);
	var examResult;
	switch(handleInfo.examStatus){
	    case 0:examResult='审核未通过';break;
	    case 1:examResult='审核通过';break;
	    default:examResult='未审核';break;
	};
	if(handleInfo.examStatus==1){
		$('#examResult').html(examResult);
		var isCharge;
		switch(handleInfo.isCharge){
		    case 0:isCharge='不收费';break;
		    case 1:isCharge='收费';break;
		    default:isCharge='未确定';break;
		};
		$('#isCharge').html(isCharge);
		$('#charge').html(handleInfo.charge);
		
		setRepairerInfo(handleInfo.repairerId);
		$("#fixTime").html(handleInfo.fixTime);
	}
	$('#handleTime').html(handleInfo.updateTime);
	$('#remark').html(handleInfo.remark);
	
}
//获取并设置工人信息
function setRepairerInfo(workerId){
	$.get(basePath+'/findWorker/'+workerId,'',function(data){
		if(data.code==0){
			$('#repairer').html(data.row.name);
			$('#repairerPhone').html(data.row.tel);
		}
	});
}
//图片上传
//图片上传
var formatErrorMsg = "文件类型错误";
var fileOverMsg = "文件大小不能超过100M";
var fileMaxSize = 100*1024*1024; //100M

function initUpload(){
   $(".file_upload").fileupload({
                url: basePath+"file/upload",
                dataType: 'json',
                autoUpload: true,
                maxFileSize: fileMaxSize,
                acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            }).on('fileuploadadd', function (e, data) {
                //添加文件
                name = data.files[0].name;
            }).on('fileuploadprocessalways', function (e, data) {
                //文件校验
                if(data.files.error){
                    $(this).removeAttr("disabled");
                    if(data.files[0].error=="File type not allowed"){
                        alert(formatErrorMsg);
                    }
                    if(data.files[0].error=="File is too large"){
                        alert(fileOverMsg);
                    }
                }
            }).on('fileuploadsubmit', function (e, data) {
                //提交前
                //$("#attName").html(data.files[0].name);
                //data.formData = {'fileName': $("#attName").val(),'pathName':'case'};
            }).on('fileuploaddone', function (e, data) {
                //上传完成
                var code = data.result.code;
                var result = data.result.row;
                if(code == 0){
                	$(this).parents(".li_photo").find('input[type="hidden"]').val(result.id);
                	$(this).parents(".add_span").replaceWith('<img src="'+result.webPath+'" class="photo">')
                }else{
                    alert(data.result.msg);
                }
            });
}

//修改报修订单信息
function updateFixOrder(){
	if(validate()==0)
		return false;
	var photoStr=getPhotoIds();
	
	$.ajax({
		url:basePath+'fixOrder/updateOrder',
		method:'post',
		dataType:'json',
		data:{
			fixOrderId:$('#fixOrderId').html(),
			article:$('#article').val(),
		    description:$('#description').val(),
		    photoStr:photoStr,
		    accountId:$('#accountId').val(),
		    name:$('#name').val(),
		    phone:$('#phone').val(),
		    address:$('#address').val(),
		    remark:$('#userRemark').val(),
		},
		success:function(data){
			if(data.code==0){
				alert("更新成功")
				fixOrderListTopic();
			}else{
				alert("更新失败")
			}
		}
	});
}

//校验
function validate(){
	var article=$.trim($('#article').val());
    var des=$.trim($('#description').val());
    var accountId=$.trim($('#accountId').val());
    var name=$.trim($('#name').val());
    var phone=$.trim($('#phone').val());
    var address=$.trim($('#address').val());
    if(article==""||article==undefined){
    	alert("报修物品不能为空");
    	return 0;
    }
    if(des==""||des==undefined){
    	alert("报修信息不能为空");
    	return 0;
    }
    if(accountId==""||accountId==undefined){
    	alert("账户信息过期");
    	return 0;
    }
    if(name==""||name==undefined){
    	alert("报修人不能为空");
    	return 0;
    }
    if(phone==""||phone==undefined){
    	alert("电话不能为空");
    	return 0;
    } 
    if(address==""||address==undefined){
    	alert("地址不能为空");
    	return 0;
    }
    return 1;
}


//获取新上传图片
function getPhotoIds(){
	var str="";
	var photoNum=0;
	$('.photo_div').each(function(){
		if($(this).find('img').length>0)
			photoNum++;
	});
	for(var i=oldPhotoNum+1;i<=photoNum;i++){
		var id=$('#photo'+i).parent().find('input[type="hidden"]').val();
		if(id!=""&&id!=undefined){
		   str+=id;
		   str+=","
		}
	}
	str=str.substring(0,str.length-1);
	if(str=="")
		return null;
	return str;
}

